package com.wei.ssm.service.impl;

import com.wei.ssm.dao.IRoleDao;
import com.wei.ssm.dao.IUserDao;
import com.wei.ssm.dao.IpermissionDao;
import com.wei.ssm.domain.Permission;
import com.wei.ssm.domain.Role;
import com.wei.ssm.service.IPermissionService;
import com.wei.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao iRoleDao;

    @Autowired
    private IUserDao iUserDao;

    @Autowired
    private IpermissionDao ipermissionDao;

    @Override
    public List<Role> findAll() {
        return iRoleDao.findAll();
    }

    @Override
    public void save(Role role){
        iRoleDao.save(role);
    }

    @Override
    public Role findById(int id) throws Exception{
        return iRoleDao.findById(id);
    }

    @Override
    public void deleteById(int id) {
        //删除用户和角色的关系
        iUserDao.deleteById(id);
        //删除角色和权限的关系
        ipermissionDao.deleteById(id);
        //删除角色
        iRoleDao.deleteById(id);
    }

    @Override
    public List<Permission> findOtherPermiss(int id) {
        return iRoleDao.findOtherPermission(id);
    }

    @Override
    public void addPermissionToRole(int roleId, int[] ids) {
        for (int id:ids
             ) {
            iRoleDao.addPermissionToRole(roleId,id);
        }
    }
}
